package project.controller;

import javax.servlet.http.HttpServlet;
import org.json.JSONObject;
import project.dao.ProjectDao;
import project.entity.Project;
import user.entity.User;
import java.util.Date;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ProjectAddServlet extends HttpServlet{
    private final ProjectDao projectDao = new ProjectDao();
    private static final long serialVersionUID = 1;

    public ProjectAddServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        System.out.println("进入了ProjectAddServlet");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        Project project = new Project();
        project.setId(Integer.parseInt(request.getParameter("projectId")));
        project.setLeader(request.getParameter("leader"));
        project.setProjectName(request.getParameter("projectName"));

        System.out.println(request.getParameter("userId"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        System.out.println(userId);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
//        project.setCreateTime(request.getParameter("createTime"));
        project.setCreateTime(dateString);
        System.out.println(dateString);
        project.setDetail(request.getParameter("detail"));
        project.setState(0);//立项时默认状态为0
        projectDao.addProject(project,userId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("message","项目信息添加成功");
        System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
